package com.acdc.dlt698.builder;

import java.nio.ByteBuffer;

import com.acdc.dlt698.entity.GetRequestNormalData;
import com.acdc.dlt698.entity.GetRequestNormalFrame;
import com.acdc.dlt698.utils.FrameBuildUtils;

import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
/**
 * 获取正常数据帧构建器
 */
public class GetRequestNormalFrameBuilder implements BaseFrameBuilder<GetRequestNormalFrame> {

    @Override
    public byte[] buildFrame(GetRequestNormalFrame frame) {
        byte[] frameHead = FrameBuildUtils.buildFrameHead(frame);
        byte[] linkUserData = buildLinkUserData(frame);
        return FrameBuildUtils.buildFrame(frameHead, linkUserData);
    }

    @Override
    public byte[] buildLinkUserData(GetRequestNormalFrame frame) {
        ByteBuffer buffer = ByteBuffer.allocate(64);
        
        GetRequestNormalData data = frame.getData();
        buffer.put(data.getApdu());
        buffer.put(data.getOpera());
        buffer.put(data.getPIID());
        buffer.put(data.getOad());
        buffer.put(data.getTimeTag());

        byte[] userDataBytes = new byte[buffer.position()];
        buffer.rewind(); // 重置位置到0
        buffer.get(userDataBytes); // 从缓冲区读取到数组中
        return userDataBytes;
    }
}
